﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RTSoft.SoureCode.Control.DonGiaBan;
using DevExpress.XtraEditors.Repository;
using RTSoft.SoureCode.Control.HangHoa;
using RTSoft.Modules;
using RTSoft.SoureCode.Control.Ngay;

namespace RTSoft.SoureCode.Entity
{
    public partial class frmDonGiaBan : DevExpress.XtraEditors.XtraForm
    {
        public frmDonGiaBan()
        {
            InitializeComponent();
        }
        DataTable tbl;
        String errConnect;
        String errSQL;

        private void frmDonGiaBan_Load(object sender, EventArgs e)
        {
            try
            {
                this.InitGrid();
            }
            catch
            {
                Library.ShowMessage("Lỗi dữ liệu đầu vào!");
            }
        }
        public void InitGrid()
        {
            tbl = new DataTable();
            errConnect = "";
            errSQL = "";
            dongia_banctrl ctrl = new dongia_banctrl();
            ctrl.getData(ref errConnect, ref errSQL, ref tbl);
            GridControl1.DataSource = tbl;
            GridView1.Columns["ID_MATHANG"].Caption = "Mặt hàng";
            GridView1.Columns["ID_NGAY"].Caption = "Ngày tạo";            
            GridView1.Columns["DONGIA"].Caption = "Đơn giá";
            GridView1.Columns["DONGIA"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            GridView1.Columns["DONGIA"].DisplayFormat.FormatString = "###,###,##0";
            //Lấy tên mặt hàng để nhập đơn giá
            RepositoryItemGridLookUpEdit riCombo = new RepositoryItemGridLookUpEdit();
            hanghoactrl itemctrl = new hanghoactrl();
            DataTable tblItem = new DataTable();
            itemctrl.getData(ref errConnect, ref errSQL, ref tblItem);
            riCombo.DataSource = tblItem;
            riCombo.ValueMember = "ID_MATHANG";
            riCombo.DisplayMember = "TEN_MATHANG";
            GridControl1.RepositoryItems.Add(riCombo);
            GridView1.Columns["ID_MATHANG"].ColumnEdit = riCombo;
            riCombo.View.Columns["ID_MATHANG"].Visible = false;
            riCombo.View.Columns["ID_NSX"].Visible = false;
            riCombo.View.Columns["ID_XX"].Visible = false;
            riCombo.View.Columns["ID_LOAI"].Visible = false;
            riCombo.View.Columns["DONVITINH"].Visible = false;
            riCombo.View.Columns["SOLUONG_MAX"].Visible = false;
            riCombo.View.Columns["SOLUONG_MIN"].Visible = false;
            riCombo.View.Columns["VAT"].Visible = false;
            riCombo.View.Columns["TRANGTHAI"].Visible = false;

            //Load thời gian lên
            riCombo = new RepositoryItemGridLookUpEdit();
            ngayctrl datectrl = new ngayctrl();
            DataTable tblDate = new DataTable();
            datectrl.getData(ref errConnect, ref errSQL, ref tblDate);
            riCombo.DataSource = tblDate;
            riCombo.DisplayMember = "NGAY";
            riCombo.ValueMember = "ID_NGAY";            
            GridControl1.RepositoryItems.Add(riCombo);
            riCombo.View.Columns["ID_NGAY"].Visible = false;
            GridView1.Columns["ID_NGAY"].ColumnEdit = riCombo;
            GridView1.BestFitColumns();
        }

        #region "Event Button"
        private void btnExcel_Click(object sender, EventArgs e)
        {
            Library.ExportToExcel(this, "Đơn giá bán", GridView1);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (GridView1.FocusedRowHandle >= 0)
                {
                    int id = Convert.ToInt32(GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["ID_MATHANG"].FieldName));
                    errConnect = "";
                    errSQL = "";
                    string strName = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["ID_NGAY"].FieldName).ToString();
                    Boolean result = Library.ShowQuestionMessage("Bạn có chắc xóa " + strName + " ?");
                    if (result == false)
                    {
                        return;
                    }
                    dongia_banctrl ctrl = new dongia_banctrl();
                    ctrl.Delete(ref errConnect, ref errSQL, id);
                    if (errSQL != "")
                    {
                        Library.ShowMessage("Không thể xóa!");
                    }
                    else
                    {
                        GridView1.DeleteSelectedRows();
                        GridView1.RefreshData();
                    }
                }
            }
            catch (Exception ex)
            {
                Library.ShowMessage(ex.ToString());
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void GridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            String colTemp = "";
            try
            {
                // Bắt lỗi dữ liệu rỗng
                for (int i = 0; i < GridView1.Columns.Count - 1; i++)
                {
                    if (GridView1.Columns[i].Visible == true)
                    {
                        colTemp = (GridView1.Columns[i].Name).Substring(3);
                        Object value = GridView1.GetRowCellValue(e.RowHandle, colTemp);
                        if ((colTemp == "ID_MATHANG") || (colTemp == "ID_NGAY") || (colTemp == "DONGIA"))
                        {
                            if (value == System.DBNull.Value)
                            {
                                e.ErrorText = "Hãy Nhập " + GridView1.Columns[i].Caption;
                                e.Valid = false;
                                GridView1.FocusedColumn = GridView1.Columns[colTemp];
                                GridView1.ShowEditor();
                                return;
                            }
                        }
                    }
                }
                dongia_banctrl ctrl = new dongia_banctrl();
                dongia_baninfo info = new dongia_baninfo();

                errConnect = "";
                errSQL = "";
                DataRow row = GridView1.GetDataRow(e.RowHandle);
                info.ID_MATHANG = Convert.ToInt32(RTValidate.CheckNull(row["ID_MATHANG"]));
                info.ID_NGAY = Convert.ToInt32(RTValidate.CheckNull(row["ID_NGAY"]));
                info.DONGIA = Convert.ToInt32(RTValidate.CheckNull(row["DONGIA"]));
                int checkExist = ctrl.checkExist(ref errConnect, ref errSQL, info.ID_MATHANG);
                if (checkExist != 0)
                {
                    Boolean result = Library.ShowQuestionMessage("Dữ liệu có sự thay đổi có chắc sửa");
                    if (result != false)
                    {
                        ctrl.Update(ref errConnect, ref errSQL, info);
                    }
                }
                else
                {
                    ctrl.Insert(ref errConnect, ref errSQL, info);
                }
                this.InitGrid();
            }
            catch (Exception ex)
            {
                Library.ShowMessage(ex.ToString());
            }
        }

        
    }
}